class RemarkController {
    def authenticateService
    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        [remarkInstanceList: Remark.list(params), remarkInstanceTotal: Remark.count()]
    }

    def create = {
        def remarkInstance = new Remark()
        remarkInstance.properties = params
        return [remarkInstance: remarkInstance]
    }

    def save = {
        def remarkInstance = new Remark(params)
        remarkInstance.user = authenticateService.userDomain()
        if (remarkInstance.save(flush: true)) {
            flash.message = "添加成功"
            redirect(action: "show",controller:"post",id:remarkInstance.post.id)
        }
        else {
            render(view: "../post/show", model: [postInstance: Post.get(params.post.id),remarkInstance: remarkInstance])
        }
    }

    def show = {
        def remarkInstance = Remark.get(params.id)
        if (!remarkInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'remark.label', default: 'Remark'), params.id])}"
            redirect(action: "list")
        }
        else {
            [remarkInstance: remarkInstance]
        }
    }

    def edit = {
        def remarkInstance = Remark.get(params.id)
        if (!remarkInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'remark.label', default: 'Remark'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [remarkInstance: remarkInstance]
        }
    }

    def update = {
        def remarkInstance = Remark.get(params.id)
        if (remarkInstance) {
            remarkInstance.properties = params
            if (!remarkInstance.hasErrors() && remarkInstance.save(flush: true)) {
                flash.message = "更新成功"
                render(view: "../post/show", model: [postInstance: Post.get(remarkInstance.post.id)])
            }
            else {
                render(view: "../post/show", model: [postInstance: Post.get(remarkInstance.post.id),remarkInstance: remarkInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'remark.label', default: 'Remark'), params.id])}"
            render(view: "../post/show", model: [postInstance: Post.get(remarkInstance.post.id)])
        }
    }

    def delete = {
        def remarkInstance = Remark.get(params.id)
        if (remarkInstance) {
            try {
                remarkInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'remark.label', default: 'Remark'), remarkInstance])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'remark.label', default: 'Remark'), remarkInstance])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'remark.label', default: 'Remark'), params.id])}"
            redirect(action: "list")
        }
    }
}
